//index.js
import { $wuxDialog } from '../../dist/index'
const app = getApp()

Page({
  data: {
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    takeSession: false,
    requestResult: '',
    current: 'tab1'
  },
  

  onLoad: function() {
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res);
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

  onGetUserInfo: function(e) {
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
    }
  },

  onGetOpenid: function() {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
        wx.navigateTo({
          url: '../userConsole/userConsole',
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        wx.navigateTo({
          url: '../deployFunctions/deployFunctions',
        })
      }
    })
  },

  // 上传图片
  doUpload: function () {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        wx.showLoading({
          title: '上传中，别着急行不',
        })

        const filePath = res.tempFilePaths[0]
        
        // 上传图片
        const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)

            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
            
            wx.navigateTo({
              url: '../storageConsole/storageConsole'
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      }
    })
  },

  //生物识别
  whoIsThat: function () {
    wx.checkIsSupportSoterAuthentication({
      success(res) {
        console.log(res)
        // res.supportMode = [] 不具备任何被SOTER支持的生物识别方式
        // res.supportMode = ['fingerPrint'] 只支持指纹识别
        // res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别
      },
      complete(res) {
        console.log(res);
      }
    })
  },


  //九宫格点击事件 
  clickOneOfNine:function () {

  },
  // 暂未开放！@
  nothing(e) {
    
    switch(e.target.dataset.id) {
      case "1":
      wx.navigateTo({
        url:'../polularScience/polularScience'
      })
      break;
      case "2":
      console.log(e.target.dataset.id);
      break;
      case "3":

      break;
      case "4":

      break;
      case "5":
        
      break;
      case "6": 

      break;
      case "7":
      
      break;
      case "8":

      break;
      case "9":
      $wuxDialog().confirm({
        resetOnClose: true,
        closable: true,
        title: '暂未开放！！',
        content: '你把屏幕点碎也进不来，真的。',
        onConfirm(e) {
            console.log('疯狂点击确认')
        },
        onCancel(e) {
            console.log('疯狂点击取消')
        },
      })
      break;
    };
  },

  //底部tab切换
  // tabChange:function (e) {
  //   console.log(e);
  //   if(e.detail.key === 'tab1'){
  //     wx.switchTab({
  //       url:'../index/index'}
  //     )
  //   }else if(e.detail.key === 'tab2'){
  //     wx.switchTab({
  //       url:'../my/my'}
  //     )
  //   }
  //   this.setData({
  //     current: e.detail.key,
  //   })
  //   console.log(this.data.current);
  // },
})
